import random
SIZE = 9
UNIT_SIZE = 3

board = [[0 for r in range(SIZE)] for c in range(SIZE)]

nums = list(range(1, SIZE + 1))
# random.shuffle(nums)

def RollUp(unit):
    return unit[1:]+[unit[0]]

def RollDown(unit):
    return [unit[-1]]+unit[:-1]

def RollLeft(unit):    
    return [RollUp(x) for x in unit]

def RollRight(unit):
    return [RollDown(x) for x in unit]

def unit_copy(row, col, unit):
    global board
    for r in range(UNIT_SIZE):
        for c in range(UNIT_SIZE):
            board[row * 3 + r][col * 3 + c] = unit[r][c]

def main():
    global board
    bu = [[nums[r * UNIT_SIZE + c] for c in range(UNIT_SIZE)] for r in range(UNIT_SIZE)]

    l_bu = RollUp(bu)
    r_bu = RollDown(bu)
    t_bu = RollLeft(bu)
    b_bu = RollRight(bu)

    unit_copy(1, 1, bu)
    unit_copy(1, 0, l_bu)
    unit_copy(1, 2, r_bu)
    unit_copy(0, 1, t_bu)
    unit_copy(2, 1, b_bu)
    unit_copy(0, 0, RollLeft(l_bu))
    unit_copy(2, 0, RollRight(l_bu))
    unit_copy(0, 2, RollLeft(r_bu))
    unit_copy(2, 2, RollRight(r_bu))
    
    for r in range(SIZE):
        print(board[r])
main()
